package com.smart.sso.server.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.smart.mvc.service.mybatis.impl.ServiceImpl;
import com.smart.sso.server.dao.LoginDatetimeLimitDao;
import com.smart.sso.server.model.LoginDatetimeLimit;
import com.smart.sso.server.service.LoginDatetimeLimitService;

@Service("LoginDatetimeLimitService")
public class LoginDatetimeLimitServiceImpl extends ServiceImpl<LoginDatetimeLimitDao, LoginDatetimeLimit, Integer> implements LoginDatetimeLimitService {

	@Autowired
	public void setDao(LoginDatetimeLimitDao dao) {
		this.dao = dao;
	}
	
	public void deleteByUserIds(List<Integer> idList) {
		dao.deleteByUserIds(idList);
	}
	
	public List<LoginDatetimeLimit> findByUserId(Integer userId) {
		return dao.findByUserId(userId);
	}
	
	public boolean isDatetimePermitted(Integer userId, Date now) {
		boolean dateOk = false;
		boolean timeOk = false;
		boolean isSetDate = false;
		boolean isSetTime = false;
		boolean result = true;
		List<LoginDatetimeLimit> datetimeList = this.findByUserId(userId);
		for(int i = 0; i<datetimeList.size(); i++)
		{
			LoginDatetimeLimit loginDatetimeLimit = datetimeList.get(i);
			Date startDatetime = loginDatetimeLimit.getStartDatetime();
			Date endDatetime = loginDatetimeLimit.getEndDatetime();
			Calendar startCalendar = Calendar.getInstance();
			startCalendar.setTime(startDatetime);
			Calendar endCalendar = Calendar.getInstance();
			endCalendar.setTime(endDatetime);
			Calendar nowCalendar = Calendar.getInstance();
			nowCalendar.setTime(now);
			
			SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd");
			String justDate = ymd.format(endCalendar.getTime());
			
			if(justDate.equalsIgnoreCase("0001-01-01"))
			{
				isSetTime = true;
				Calendar tmpCalendar = nowCalendar;
				nowCalendar.set(1, 0, 1);
				if(tmpCalendar.after(startCalendar) && tmpCalendar.before(endCalendar))
				{
					timeOk = true;
				}
			}
			else
			{
				isSetDate = true;
				if(nowCalendar.after(startCalendar) && nowCalendar.before(endCalendar))
				{
					dateOk = true;
				}
			}
		}
		if(isSetTime && !isSetDate)	//只设置了每天登陆时段，没设置日期范围。
		{
			return timeOk;	//以每天登陆时段判断结果作为返回值
		}
		else if(isSetTime && isSetDate)	//同时设置了每天登陆时段和日期范围。
		{
			return timeOk && dateOk;	//以两项判断结果
		}
		else if(!isSetTime && isSetDate)	//只设置了日期范围，没设置每天登陆时段。
		{
			return dateOk;	//以日期范围判断结果作为返回值
		}
		else	//都没设置
			return result;	//默认不受任何限制
	}
	
    @Transactional
	public void deleteById(List<Integer> idList) {
    	dao.deleteByUserIds(idList);
    }
    
    public void saveDateTimes(Integer userId, String dateTimes)
    {
    	if (dateTimes != null) {
    		String postfix = ":00";
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    		String[] strs = dateTimes.split(",");
    		for (int i = 0, length = strs.length; i < length; i++) {
    			String[] dts = strs[i].split("#");
    			if(dts.length!=2)
    			{
    				break;
    			}
    			LoginDatetimeLimit loginDatetimeLimit = new LoginDatetimeLimit();
    			try {
    				dts[0] = dts[0] + postfix;
					loginDatetimeLimit.setStartDatetime(sdf.parse(dts[0]));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
    			try {
    				dts[1] = dts[1] + postfix;
					loginDatetimeLimit.setEndDatetime(sdf.parse(dts[1]));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
    			loginDatetimeLimit.setIsTime(false);
    			loginDatetimeLimit.setUserId(userId);
    			save(loginDatetimeLimit);
    		}
    	}
    }
    
    public void saveTimes(Integer userId, String times)
    {
    	if (times != null) {
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    		String prefix = "0001-01-01 ";
    		String postfix = ":00";
    		String[] strs = times.split(",");
    		for (int i = 0, length = strs.length; i < length; i++) {
    			String[] ts = strs[i].split("-");
    			if(ts.length!=2)
    			{
    				break;
    			}
    			LoginDatetimeLimit loginDatetimeLimit = new LoginDatetimeLimit();
    			try {
    				ts[0] = prefix + ts[0] + postfix;
					loginDatetimeLimit.setStartDatetime(sdf.parse(ts[0]));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
    			try {
    				ts[1] = prefix + ts[1] + postfix;
					loginDatetimeLimit.setEndDatetime(sdf.parse(ts[1]));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
    			loginDatetimeLimit.setIsTime(true);
    			loginDatetimeLimit.setUserId(userId);
    			save(loginDatetimeLimit);
    		}
    	}
    }
    
    public void save(LoginDatetimeLimit t) {
		super.save(t);
	}
}
